<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/28
 * Time: 16:08
 */

namespace common\sources\write\taskcommod;

use common\models\craftsman\TaskCommod;
use Yii;

class TaskCommodAccess implements ITaskCommodAccess
{
    public function AuditTask($data) {
        $task = TaskCommod::findOne(['id' => $data['id']]);
        if ($task) {
            $task->task_status = $data['status'];
            $task->audit_operator = $data['operator'];
            $task->reason = $data['reason'];
            $task->audit_time = time();
            if ($task->save()) {
                return true;
            }
        }
        return false;
    }

    public function IsPrint($data) {
        $task = TaskCommod::findOne(['id' => $data['id']]);
        if ($task) {
            $task->is_print = $data['is_print'];
            if ($task->save()) {
                return true;
            }
        }
        return false;
    }

    public function ModifyNotic($data) {
        $task = TaskCommod::findOne(['id' => $data['id']]);
        if ($task) {
            $task->prompt = $data['notic'];
            if ($task->save()) {
                return true;
            }
        }
        return false;
    }

    public function ModifySpu($data) {
        $task = TaskCommod::findOne(['id' => $data['id']]);
        if ($task) {
            $task->commod_link = $data['commod_link'];
            $task->title = $data['title'];
            $task->main_url1 = $data['main_url1'];
            $task->main_url2 = $data['main_url2'];
            $task->colour = $data['colour'];
            $task->size = $data['size'];
            if ($task->save()) {
                return true;
            }
        }
        return false;
    }

    public function ModifyTaskKeyWords($data) {
        $task = TaskCommod::findOne(['id' => $data['task_id']]);
        if ($task) {
            $task->range_price = $data['range_price'];
            $task->ship_addr = $data['ship_addr'];
            if ($task->save()) {
                return true;
            }
        }
        return false;
    }

    public function taskCommodComplete($data)
    {
        $sql = 'UPDATE ' . TaskCommod::tableName() . ' SET task_status = :task_status,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':task_status' => $data['task_status'],
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }

	public function modifyTaskCommod($data) {
		$sql = 'UPDATE ' . TaskCommod::tableName() . ' SET task_answer = :task_answer,surplus_task_num = :surplus_task_num,update_time = :update_time WHERE id = :id ';
		return Yii::$app->db->createCommand($sql)
			->bindValues([
				':task_answer'      => $data['task_answer'],
				':surplus_task_num' => $data['surplus_task_num'],
				':update_time'      => time(),
				':id'               => $data['id']
			])->execute();
	}

}